package com.woniuxy.dudugua.service.impl;

import com.woniuxy.dudugua.entity.*;
import com.woniuxy.dudugua.mapper.OrderMapper;
import com.woniuxy.dudugua.mapper.ReturnGoodsMapper;
import com.woniuxy.dudugua.mapper.ReturnProcessMapper;
import com.woniuxy.dudugua.service.ReturnGoodsService;
import com.woniuxy.dudugua.util.view.ReturnGoodsAndProductView;
import com.woniuxy.dudugua.util.view.ReturnProductView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ReturnGoodsServiceImpl implements ReturnGoodsService {
    @Autowired
    private ReturnGoodsMapper returnGoodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ReturnProcessMapper returnProcessMapper;
    @Override
    public List<ReturnProductView> selectProductByOrderId(int orderId) {
        List<ReturnProductView> e = returnGoodsMapper.selectByOrderId(orderId);
        return e;
    }

    @Override
    public ReturnGoods selectByOrderId(int orderId) {
        ReturnGoodsExample e = new ReturnGoodsExample();
        ReturnGoodsExample.Criteria c = e.createCriteria();
        c.andOrderIdEqualTo(orderId);
        List<ReturnGoods> list = returnGoodsMapper.selectByExample(e);
        if(list.size() != 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int insertReturnGoods(ReturnGoods returnGoods) {
        int insert = returnGoodsMapper.insert(returnGoods);
        return insert;
    }

    @Override
    public List<ReturnGoodsAndProductView> selectByUserId(int userid) {
        List<ReturnGoodsAndProductView> list = returnGoodsMapper.selectByUserId(userid);
        return list;
    }

    @Override
    public ReturnGoods selectById(int id) {
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(id);
        return returnGoods;
    }

    @Override
    public Order selectOrderByReturnId(int returnid) {
        List<Order> orders = orderMapper.selectOrderByReturnId(returnid);
        if(orders.size() != 0){
            return orders.get(0);
        }
        return null;
    }

    @Override
    public ReturnGoodsAndProductView selectReturnProductViewByReturnId(int returnid) {
        List<ReturnGoodsAndProductView> list = returnGoodsMapper.selectReturnProductViewByReturnId(returnid);
        if(list.size() != 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateTrackNumberByReturnId(int returnId, String trackNumber) {
        ReturnProcessExample e = new ReturnProcessExample();
        ReturnProcessExample.Criteria c = e.createCriteria();
        c.andReturnGoodsIdEqualTo(returnId);
        ReturnProcess returnProcess = returnProcessMapper.selectByExample(e).get(0);
        returnProcess.setCourierNumber(trackNumber);
        returnProcess.setStatus(2);
        int i = returnProcessMapper.updateByPrimaryKey(returnProcess);
        ReturnGoods returnGoods = returnGoodsMapper.selectByPrimaryKey(returnId);
        returnGoods.setStatus(4);
        int i1 = returnGoodsMapper.updateByPrimaryKey(returnGoods);
        return i+i1;
    }
}
